<?php
/**
 * Pertyler
 * Database Model
 * Database Classes
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

class Database
{
	private $name;
	
	private $address;

	private $port;

	private $user;

	private $password;

	private static $object;

	public function __Construct($name,
			$address,
			$port,
			$user,
			$password)
	{
		$this->name = $name;
		
		$this->address = $address;

		$this->port = $port;

		$this->user = $user;

		$this->password = $password;

		if(isset(self::$object))
			throw new DatabaseException('There can be only one database connection.');

		self::$object = $this;
	}

	private $postgre;

	public static function Postgre()
	{
		if(empty(self::$object))
			throw new DatabaseException('No database connection.');

		if(empty(self::$object->postgre))
			self::$object->postgre = new \Pertyler\Postgre(self::$object->name,
					self::$object->address,
					self::$object->port,
					self::$object->user,
					self::$object->password);

		return self::$object->postgre;
	}
}

class DatabaseException extends \Exception
{
}